package com.j283_5.wisdom_park.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.j283_5.wisdom_park.common.result.ResultData;
import com.j283_5.wisdom_park.common.utils.HashMapUtils;
import com.j283_5.wisdom_park.mapper.RoomMapper;
import com.j283_5.wisdom_park.mapper.TenementMapper;
import com.j283_5.wisdom_park.pojo.ContractTable;
import com.j283_5.wisdom_park.pojo.Room;
import com.j283_5.wisdom_park.pojo.Tenement;
import com.j283_5.wisdom_park.service.TenementService;
import com.j283_5.wisdom_park.vo.asset_management.TenISVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author laoda
 * @since 2022-04-28
 */
@Service
public class TenementServiceImpl implements TenementService {
    @Resource
    private TenementMapper mapper;
    @Resource
    private RoomMapper roomMapper;
    /**
     * 多条件查询楼宇
     * @param ten_name
     * @param page
     * @return
     */
    @Override
    public ResultData findTenement(String ten_name, Page<Tenement> page) {
        QueryWrapper<Tenement> tenementQueryWrapper = new QueryWrapper<>();
        if (ten_name!=null){
            tenementQueryWrapper.like("ten_name",ten_name);
        }
        Page<Tenement> tenementPage = mapper.selectPage(page, tenementQueryWrapper);

        return ResultData.ok().data(tenementPage) ;
    }

    /**
     * 查询楼宇信息
     * @return
     */
    @Override
    public ResultData findTenementById(Integer ten_Id) {
        QueryWrapper<Tenement> tenementQueryWrapper = new QueryWrapper<>();
        tenementQueryWrapper.eq("ten_Id", ten_Id);
        List<Tenement> tenements = mapper.selectList(tenementQueryWrapper);
        return ResultData.ok().data(tenements);
    }

    /**
     * 查询房间信息
     * @return
     */
    @Override
    public ResultData findRoomById(Integer ten_Id) {
        QueryWrapper<Room> roomQueryWrapper = new QueryWrapper<>();
        roomQueryWrapper.eq("ten_Id",ten_Id);
        List<Room> rooms = roomMapper.selectList(roomQueryWrapper);
        return ResultData.ok().data(rooms);
    }

    /**
     * 楼宇新增
     * @param tenement
     * @return
     */
    @Override
    public ResultData insertTenement(Tenement tenement) {
        int insert = mapper.insert(tenement);
        if (insert>0){
            return ResultData.ok().message("插入成功");
        }else {
            return ResultData.error().message("插入失败");
        }
    }

    /**
     * 楼宇删除
     * @param tenId
     * @return
     */
    @Override
    public ResultData deleteTenement(List<Integer> tenId) {
        UpdateWrapper<Tenement> TenementWrapper = new UpdateWrapper<>();
        TenementWrapper.set("ten_deleteId",1);
        TenementWrapper.in("ten_Id",tenId);
        int i = mapper.update(null, TenementWrapper);
        if(i > 0){
            return ResultData.ok().message("删除成功!");
        }
        return ResultData.error().message("删除失败！");
    }

    /**
     * 楼宇id和名字
     * @return
     */
    @Override
    public ResultData findTenId(Integer ten_Id) {
        List<TenISVo> tenList = mapper.findTenId(ten_Id);
        return ResultData.ok().data(tenList);
    }

}
